package com.jfinal_demo.app.exceptions;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal_demo.app.DataWrapper;

public class ExceptionInterceptor implements Interceptor {
    @Override
    public void intercept(Invocation invocation) {
        if (invocation.isActionInvocation()) {
            try {
                invocation.invoke();
            } catch (Exception ex) {
                String message = ex.getMessage();
                if (message == null) message = "操作失败";
                invocation.getController().renderJson(DataWrapper.fail(message));
            }
        } else {
            invocation.invoke();
        }
    }
}
